#!/bin/bash
###############################################################################
# @用例ID: 20230524-000005-979367466
# @用例名称: tc-docker-13
# @用例级别: 3
# @用例标签:
# @用例类型: 功能
###############################################################################
[ -z "$TST_TS_TOPDIR" ] && {
    TST_TS_TOPDIR="$(realpath "$(dirname "$0")/..")"
    export TST_TS_TOPDIR
}
source "${TST_TS_TOPDIR}/tst_common/lib/common.sh" || exit 1
###############################################################################

OS_ARCH='amd64'

PROXY=${https_proxy}

DOCKER_VERSION='23.0.1'
BUILDX_VERSION='v0.10.2'
CONTAINERD_VERSION='1.6.16'
RUNC_VERSION='v1.1.4'

g_tmpdir="$(mktemp -d)"

tc_setup() {
    msg "this is tc_setup"
    sudo yum install wget -y
    echo ">>>>>>>>>>>>>>>>>>>>>>> Install Runc <<<<<<<<<<<<<<<<<<<<<<<<"
    wget https://github.com/opencontainers/runc/releases/download/${RUNC_VERSION}/runc.${OS_ARCH}
    sudo install -m 755 runc.amd64 /usr/local/sbin/runc
    echo ">>>>>>>>>>>>>>>>>>>>>>> Install Containerd <<<<<<<<<<<<<<<<<<<<<<<<"
    CONTAINERD_FILE=containerd-${CONTAINERD_VERSION}-linux-${OS_ARCH}.tar.gz
    wget https://github.com/containerd/containerd/releases/download/v${CONTAINERD_VERSION}/${CONTAINERD_FILE}
    sudo tar Czxvf /usr/local ./${CONTAINERD_FILE}
    wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
    sudo mv containerd.service /usr/lib/systemd/system/
    sudo systemctl daemon-reload
    sudo systemctl enable --now containerd
    sudo systemctl status containerd
    echo ">>>>>>>>>>>>>>>>>>>>>>> Install Docker <<<<<<<<<<<<<<<<<<<<<<<<"
    DOCKER_FILE=docker-${DOCKER_VERSION}.tgz
    wget https://download.docker.com/linux/static/stable/x86_64/${DOCKER_FILE}
    tar xzvf ./${DOCKER_FILE}
    sudo cp docker/* /usr/bin/
    sudo groupadd docker
    sudo usermod -aG docker $USER
    wget https://raw.githubusercontent.com/moby/moby/master/contrib/init/systemd/docker.service
    wget https://raw.githubusercontent.com/moby/moby/master/contrib/init/systemd/docker.socket
    sudo mv docker.service docker.socket /usr/lib/systemd/system/
    sudo systemctl daemon-reload
    sudo systemctl enable --now docker
    sudo systemctl status docker
    sleep 10s
    echo ">>>>>>>>>>>>>>>>>>>>>>> Install Buildx <<<<<<<<<<<<<<<<<<<<<<<<"
    BUILDX_FILE=buildx-${BUILDX_VERSION}.linux-${OS_ARCH}
    wget https://github.com/docker/buildx/releases/download/${BUILDX_VERSION}/${BUILDX_FILE}
    sudo mv ./${BUILDX_FILE} /usr/bin/buildx
    echo ">>>>>>>>>>>>>>>>>>>>>>> Set Proxy for Docker <<<<<<<<<<<<<<<<<<<<<<<<"
    sudo mkdir -p /etc/systemd/system/docker.service.d
    cat <<EOF | sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=${PROXY}"
Environment="HTTPS_PROXY=${PROXY}"
EOF
    sudo systemctl daemon-reload
    sudo systemctl restart docker
    sleep 10s
    if [ -n $PROXY ]; then
      set_proxy_for_docker
    fi
    sudo chmod 666 /var/run/docker.sock
    return 0
}

do_test() {
    msg "this is do_test"
    # @测试步骤: 命令测试
    # @预期结果: 命令测试成功
    assert_true docker pull mysql
    assert_true docker images

    assert_true docker run -itd --name db-server -e MYSQL_ROOT_PASSWORD=P2ssw0rd mysql
    assert_true docker ps -a
    assert_true docker commit -m "update" -a "aaron" db-server registry.cn-shanghai.aliyuncs.com/aaron-share/db-server

    assert_true docker rm -f db-server
    assert_true docker rmi registry.cn-shanghai.aliyuncs.com/aaron-share/db-server

    pushd $g_tmpdir
    assert_true echo 'FROM ubuntu:latest
    MAINTAINER aaron
    ARG APT=apt-get
    RUN $APT update \
      && $APT install -y curl \
      && rm -rf /var/lib/apt/lists/*
    CMD [ "curl", "-s", "http://baidu.com" ]' >Dockerfile

    assert_true docker build -t registry.cn-shanghai.aliyuncs.com/aaron-share/ubuntu-server:latest .
    popd

    return 0
}

tc_teardown() {
    msg "this is tc_teardown"
    rm -rfv "$g_tmpdir" || return 1
    return 0
}

###############################################################################
tst_main "$@"
###############################################################################
